package com.dyb.demo.test_thread.thread;

/***
 *                    .::::.
 *                  .::::::::.
 *                 :::::::::::
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 */
/**
 * 在当前线程调用thread.join()，当前线程等待thread执行结束后继续执行当前线程
 */
@SuppressWarnings("Duplicates")
public class TestJoin {

    public static void main(String[] args) {
        TestJoin testJoin = new TestJoin();
        Thread t1 = new Thread(testJoin.new MyThread(), "Thread-t1");
        Thread t2 = new Thread(testJoin.new MyThread(), "Thread-t2");
        Thread t3 = new Thread(testJoin.new MyThread(), "Thread-t3");


        try {
            t1.start();
            System.out.println(Thread.currentThread().getName() + " 等待");
            // t1.join()调用，main线程等待t1执行结束
            t1.join();

            t2.start();
            System.out.println(Thread.currentThread().getName() + " 等待");
            t2.join();

            t3.start();
            System.out.println(Thread.currentThread().getName() + " 等待");
            t3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    class MyThread implements Runnable {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "开始执行");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "执行结束");
        }
    }
}
